#include "RestClient.h"
#include "ResponseManager.h"

namespace http {
  RestClient::RestClient(const std::string &uri)
    : uri_base(uri)
  {
    curl_handle = curl_easy_init();
    curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &write_data);
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &response_body);
    curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, &response_head);
  }

  http::Response* RestClient::get(const std::string &uri) {
    curl_easy_setopt(curl_handle, CURLOPT_URL, (uri_base + uri).c_str());
    curl_easy_perform(curl_handle);

    return http::ResponseManager::GetResponseObj(response_body, response_head);
  }

  http::Response* RestClient::post(const std::string &uri, const http::Request &req) const {
  }

  size_t RestClient::write_data(void *buffer, size_t size, size_t nmemb, std::string *response) {
    response->append((const char *)buffer, size * nmemb);
    return size * nmemb;
  }

}
